package ibmetp.mis.dao;


import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.Resource;


import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResourceDao extends BaseDao{
	/**
	 * 将查询结果转换成RESOURCE类集，并保存进List集合中
	 * @author  ZHENG BAOFA
	 * @create 2011/10/8
	 */
	public List ResultSet2List(ResultSet rs) {
		List<Resource> resultList = new ArrayList<Resource>();
		try {
			while (rs != null && rs.next()) {
				Resource resource = new Resource();
				resource.setResourceId(rs.getLong("RESOURCE_ID"));
				resource.setParentId(rs.getLong("PARENT_ID"));
				resource.setResourceName(rs.getString("RESOURCE_NAME"));
				resource.setUrl(rs.getString("URL"));
				resource.setDescript(rs.getString("DESCRIPT"));
				resultList.add(resource);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}
	/**
	 * 按Id查询
	 * 
	 * @param id
	 * @return 返回资源信息
	 */
	public Resource getResourceById(Long id) {
		String sql = "select * from BAS_T_RESOURCE t where t.RESOURCE_ID=?";

		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;
		Resource resource =null;// 
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				resource=new Resource();
				resource.setResourceId(rs.getLong("RESOURCE_ID"));
				resource.setParentId(rs.getLong("PARENT_ID"));
				resource.setResourceName(rs.getString("RESOURCE_NAME"));
				resource.setUrl(rs.getString("URL"));
				resource.setDescript(rs.getString("DESCRIPT"));
				

			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resource;

	}
    

	/**
	 * 通过资源Id更新资源信息
	 * @param resource
	 */
	public void UpdateResource(Resource resource)
    {
    	String sql="update BAS_T_RESOURCE SET PARENT_ID=?,RESOURCE_NAME=?,URL=?,DESCRIPT=?,UPDATE_DATE=sysdate where RESOURCE_ID=?";
    	
    	System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			
			ps = conn.prepareStatement(sql);			
			ps.setLong(1,resource.getParentId());
			ps.setString(2,resource.getResourceName());
			ps.setString(3,resource.getUrl());
			ps.setString(4,resource.getDescript());
			ps.setLong(5, resource.getResourceId());
			
			
			
			ps.execute();
			//ResultSet rs = ps.executeQuery();
			
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    } 
	
	/**
	 * 批量添加资源
	 * @param resources
	 */
	public void addResource(List<Resource> resources)
	{
        String sql="insert into BAS_T_RESOURCE (RESOURCE_ID,PARENT_ID,RESOURCE_NAME,URL, DESCRIPT,CREATE_DATE)values (RES_SEQ.NEXTVAL,?,?,?,?,sysdate) ";

		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
			for(int i=0;i<resources.size();i++){
				Resource resource=resources.get(i);
				//ps.setLong(1, resource.getResourceId());
				if(resource.getParentId()!=null)
				ps.setLong(1,resource.getParentId());
				else ps.setLong(1,Long.valueOf("1"));
				ps.setString(2,resource.getResourceName());
				ps.setString(3,resource.getUrl());
				ps.setString(4,resource.getDescript());
			//ps.setLong(5,user.getDept().getDeptId());
			ResultSet rs = ps.executeQuery();
			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 添加资源
	 * @param resource
	 */
	public void addResource(Resource resource) {
		String sql="insert into BAS_T_RESOURCE (RESOURCE_ID,PARENT_ID,RESOURCE_NAME,URL, DESCRIPT)values (RES_SEQ.NEXTVAL,?,?,?,?) ";
		
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
		//	ps.setLong(1, resource.getResourceId());
			if(resource.getParentId()!=null)
			ps.setLong(1,resource.getParentId());
			else ps.setLong(1,Long.valueOf("1"));
			ps.setString(2,resource.getResourceName());
			ps.setString(3,resource.getUrl());
			ps.setString(4,resource.getDescript());
			//ps.setLong(5,user.getDept().getDeptId());
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 根据资源Id删除资源信息
	 * 
	 * @param id
	 */
	public void deleteById(Long id) {

		String sql = "delete from BAS_T_RESOURCE t where t.RESOURCE_ID=?";
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 批量删除资源
	 * @param resourceIds
	 */
	public void deleteByIds(String resourceIds)
	{
		String sql = "delete from BAS_T_RESOURCE t where t.RESOURCE_ID IN ("+resourceIds+")";
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;

		try {
			ps = conn.prepareStatement(sql);
			//ps.setString(1, userIds);
			ResultSet rs = ps.executeQuery();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
   
	/**
	 * 查询资源信息
	 * @param pageNo 当前页面
	 * @param pageSize页面大小
	 * @param resource 资源条件
	 * @return
	 */
	public Page getResourcePage(int pageNo, int pageSize, Object resource) {

		return this.find(resource, pageNo, pageSize, "BAS_T_RESOURCE");
	}
	/**
	 * 通过角色id获得角色已有资源
	 * @param roleId 角色ids
	 * @param pageNo 当前页面
	 * @param pageSize 页面大小
	 * @param resource 查询条件为null
	 * @created by LIN A ER at 2011-10-13
	 * @return
	 */
	public Page getRoleResPage(Long roleId,int pageNo, int pageSize, Object resource) {

		return this.find(resource, pageNo, pageSize, "(select re.* from bas_t_resource re where re.RESOURCE_ID IN(select rr.RESOURCE_ID from bas_t_role_resource rr where rr.ROLE_ID = "+roleId+"))");
	}
	/**
	 * 通过角色id获得角色为拥有资源
	 * @param roleId 角色ids
	 * @param pageNo 当前页面
	 * @param pageSize 页面大小
	 * @param resource 查询条件为null
	 * @created by LIN A ER at 2011-10-14
	 * @return
	 */
	public Page getRoleUnResPage(Long roleId,int pageNo, int pageSize, Object resource) {

		return this.find(resource, pageNo, pageSize, "(select re.* from bas_t_resource re where re.RESOURCE_ID NOT IN(select rr.RESOURCE_ID from bas_t_role_resource rr where rr.ROLE_ID = "+roleId+"))");
	}
	/**
	 * 根据资源Id获取资源信息
	 * @param resourceId
	 * @return
	 */
	public Resource getResourceById(String resourceId)
	{
		String sql = "select * from BAS_T_RESOURCE t where t.RESOURCE_ID=?";
		System.out.println(sql);
		conn = DBUtil.getConnection();
		PreparedStatement ps;
		Resource resource =null;// 
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, resourceId);
			ResultSet rs = ps.executeQuery();

			while (rs != null && rs.next()) {
				resource=new Resource();
				ps.setLong(1, resource.getResourceId());
				ps.setLong(2,resource.getParentId());
				ps.setString(3,resource.getResourceName());
				ps.setString(4,resource.getUrl());
				ps.setString(5,resource.getDescript());
				

			}
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resource;
	}
	
	/**
	 * 添加查询条件
	 */
	public Map<Integer, Object[]> getCondition(Object o) {
		Map<Integer, Object[]> paramsMap = new HashMap<Integer, Object[]>();
		Integer count = 0;
		Resource resource = (Resource) o;
		if (resource.getResourceName() != null
				&& resource.getResourceName().trim().length() > 0) {
			count++;
			paramsMap.put(count, new Object[] { " and RESOURCE_NAME like ? ",
					"%" + resource.getResourceName() + "%" });
		}
		if (resource.getUrl() != null
				&& resource.getUrl().trim().length() > 0) {
			count++;
			paramsMap.put(count, new Object[] { " and URL like ? ",
					"%" + resource.getUrl() + "%" });
		}
		return paramsMap;
	}



	

	
	

}


